package com.doubi.controller.sysDic;

import com.doubi.common.aspectj.annotation.SysLog;
import com.doubi.common.config.shiro.ShiroUtil;
import com.doubi.model.sys.TSysDic;
import com.doubi.model.sys.TSysUser;
import com.doubi.service.SysDicService;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("sysDic")
public class SysDicController {

    @Autowired
    private SysDicService sysDicService;

    /**
     * 功能描述：进入字典父级列表页面
     * @author   MrDoubi 2019-06-28 15:31
     * @return          返回页面路径
     */
    @RequiresPermissions("sysDic:list")
    @GetMapping("list")
    public String list(){
        return "web/sysDic/list";
    }

    /** 
     * 功能描述：获取字典父级分页数据
     * @author   MrDoubi 2019-06-28 15:35
     * @param page      Page对象
     * @param sysDic    TSysDic对象
     * @return          返回Page对象对象
     */
    @SysLog(title = "获取字典父级分页数据")
    @RequiresPermissions("sysDic:page")
    @PostMapping("page")
    @ResponseBody
    public PageVo<TSysDic> getPage(PageVo<TSysDic> page, TSysDic sysDic){
        return sysDicService.getPage(page,sysDic);
    }

    /**
     * 功能描述：启用禁用字典表父级
     * @author   MrDoubi 2019-06-28 15:40
     * @param sysDic    TSysDic对象
     * @return          返回AppModel对象
     */
    @SysLog(title = "启用禁用字典表父级")
    @RequiresPermissions("sysDic:updateStatus")
    @PostMapping("updateStatus")
    @ResponseBody
    public AppModel updateStatus(TSysDic sysDic){
        TSysUser sysUser = ShiroUtil.getUser();
        return sysDicService.updateStatus(sysDic, sysUser);
    }

    /**
     * 功能描述：字典表父级删除
     * @author   MrDoubi 2019-06-28 15:55
     * @param sysDic    TSysDic对象
     * @return          返回AppModel对象
     */
    @SysLog(title = "字典表父级删除")
    @RequiresPermissions("sysDic:del")
    @PostMapping("del")
    @ResponseBody
    public AppModel del(TSysDic sysDic){
        if(sysDicService.delBean(sysDic) == 1){
            return new AppModel(0,"数据操作成功！");
        }else {
            return new AppModel(-1,"数据操作失败！");
        }
    }

    /**
     * 功能描述：进入新增或修改字典父级页面
     * @author   MrDoubi 2019-06-28 16:37
     * @param sysDic    TSysDic对象
     * @param model     Model
     * @return          返回页面路径
     */
    @RequiresPermissions(value = {"sysDic:edit", "sysDic:add"}, logical = Logical.OR)
    @GetMapping("edit")
    public String edit(TSysDic sysDic, Model model){
        if(DataUtil.isNotEmpty(sysDic.getOid())){
            sysDic = sysDicService.getById(sysDic.getOid());
        }
        model.addAttribute("bean",sysDic);
        return "web/sysDic/edit";
    }

    /**
     * 功能描述：新增或修改字典父级
     * @author   MrDoubi 2019-06-28 16:53
     * @param sysDic    TSysDic对象
     * @return          返回AppModel对象
     */
    @SysLog(title = "新增或修改字典父级")
    @RequiresPermissions("sysDic:saveOrUpdate")
    @PostMapping("saveOrUpdate")
    @ResponseBody
    public AppModel saveOrUpdate(TSysDic sysDic){
        TSysUser sysUser = ShiroUtil.getUser();
        return sysDicService.saveOrUpdate(sysDic, sysUser);
    }

    /**
     * 功能描述：更新字典表缓存
     * @author   MrDoubi 2019-06-29 10:32
     * @return      返回AppModel对象
     */
    @SysLog(title = "更新字典表缓存")
    @RequiresPermissions("sysDic:updateRedisDic")
    @PostMapping("updateRedisDic")
    @ResponseBody
    public AppModel updateRedisDic(){
//        return sysDicService.updateRedisDic();
        return null;
    }


    @RequiresPermissions("sysDic:information")
    @GetMapping("information")
    public String information(Model model){
        return "web/sysDic/edit";
    }

}
